using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEquipInfo : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIEquipInfo),
            layer = EUILayer.Normal,
            isFullScreen = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIEquipInfo
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject topLeftBack { protected set; get; }
            public UnityEngine.GameObject toggleTab { protected set; get; }
            public UnityEngine.RectTransform panel { protected set; get; }
            public UnityEngine.GameObject equipDetail { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonLock { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage buttonLockImg { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv0);
                this.topLeftBack = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleTab", out var __tbv1);
                this.toggleTab = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("panel", out var __tbv2);
                this.panel = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("equipDetail", out var __tbv3);
                this.equipDetail = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonLock", out var __tbv4);
                this.buttonLock = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("buttonLockImg", out var __tbv5);
                this.buttonLockImg = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region fields & properties
        ListScrollAdapter<UIN_EquipInfoTab> tabs = null;
        UIN_EquipInfoDetail detail = null;

        UD_EquipInfo udEquip => _data as UD_EquipInfo;
        bool isLock = false;
        UIBaseNode currView = null;

        protected UIB_UIEquipInfo ui { get; set; }
        private UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIEquipInfo();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.tabs = this.InitListScroll<UIN_EquipInfoTab>(this.ui.toggleTab, this.OnTabChanged);
            this.tabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTabSelected);
            this.detail = this.InitChildNode<UIN_EquipInfoDetail>(this.ui.equipDetail);
            this.ui.buttonLock.onClick.AddListener(this.OnClickLock);
            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        void OnClickLock()
        {
            this.SendChangeLock().Forget();
        }

        async UniTaskVoid SendChangeLock()
        {
            bool isLock = !this.isLock;
            this.isLock = isLock;
            var result = await EquipmentModule.NetCore.CS_lockEquip(this.udEquip.equipItem.id, isLock);
            if (result.IsSuccess)
            {
                this.ViewBtn();
                this.ViewCurrPanel();
            }
        }

        protected override void OnDispose()
        {
            this.tabs = null;
            this.detail = null;
            this.currView = null;
            this.isLock = false;
        }

        protected override void OnShow(UIBaseData data)
        {
            this.Refresh((UD_EquipInfo)data);
        }

        #region UIMsgDispatcher

        protected override void AddMsgListeners()
        {
            uiMsgDispatcher.AddListener(MsgEventType.EquipSkinChanged, this.OnMsgEquipSkinChanged);
        }

        void OnMsgEquipSkinChanged()
        {
            this.ViewCurrPanel();
        }
        #endregion

        public void ChangeEquip(bool isAdd)
        {
            int equipIndex = this.udEquip.equipIndex;
            int index = 0;
            if (isAdd)
                index = Math.Min(equipIndex + 1, this.udEquip.equipCount);
            else
                index = Math.Max(equipIndex - 1, 0);

            if (index == equipIndex)
                return;

            this.udEquip.ChangeEquip(index);
            this.Refresh(this.udEquip);
        }

        void Refresh(UD_EquipInfo udEquip)
        {
            this.isLock = udEquip.equipItem.isLock;

            this.ViewTab();
            this.ViewBtn();
        }

        void ViewBtn()
        {
            //this.ui.buttonLockImg.indexSwitchSprite = this.isLock ? 1 : 0;
        }

        void ViewTab()
        {
            int cnt = (int)UIEquipInfoTabType.MaxLength;
            var equipData = this.udEquip.equipItem;
            if (equipData.star == 1 && equipData.level == 1)
            {
                cnt--;
                if (this.udEquip.viewType == UIEquipInfoTabType.Reborn)
                    this.udEquip.viewType = UIEquipInfoTabType.Details;
            }
            this.tabs.RefillCells(cnt, (int)this.udEquip.viewType);
        }

        void OnTabChanged(UIN_EquipInfoTab item, int index)
        {
            item.Show(index, APIUIEquip.GetInfoTabName(index));
        }

        void OnTabSelected(int index, UIN_EquipInfoTab item)
        {
            this.udEquip.viewType = (UIEquipInfoTabType)index;
            if (this.currView != null)
                this.currView.Hide();
            this.currView = null;
            this.ViewCurrPanel();
        }

        void ViewCurrPanel()
        {
            switch (this.udEquip.viewType)
            {
                case UIEquipInfoTabType.Details:
                    this.ViewDetails();
                    break;
                case UIEquipInfoTabType.UpLv:
                    this.ViewUpLv().Forget();
                    break;
                case UIEquipInfoTabType.UpStar:
                    this.ViewUpStar().Forget();
                    break;
                case UIEquipInfoTabType.Reborn:
                    this.ViewReborn().Forget();
                    break;
                default:
                    break;
            }
        }

        void ViewDetails()
        {
            this.currView = this.detail;
            this.detail.Show(this.udEquip);
        }

        async UniTaskVoid ViewUpLv()
        {
            string assetPath = APIUIEquip.GetInfoNodePrefabAssetPath(((int)UIEquipInfoTabType.UpLv));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_EquipInfoLevel panel = await this.InitChildNodeAsync<UIN_EquipInfoLevel>(assetPath, this.ui.panel);
            this.currView = panel;
            panel?.Show(this.udEquip);
        }

        async UniTaskVoid ViewUpStar()
        {
            string assetPath = APIUIEquip.GetInfoNodePrefabAssetPath(((int)UIEquipInfoTabType.UpStar));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_EquipInfoStar panel = await this.InitChildNodeAsync<UIN_EquipInfoStar>(assetPath, this.ui.panel);
            this.currView = panel;
            panel?.Show(this.udEquip);
        }

        /// <summary>
        /// 重造
        /// </summary>
        async UniTaskVoid ViewReborn()
        {
            string assetPath = APIUIEquip.GetInfoNodePrefabAssetPath(((int)UIEquipInfoTabType.Reborn));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_EquipInfoReborn panel = await this.InitChildNodeAsync<UIN_EquipInfoReborn>(assetPath, this.ui.panel);
            this.currView = panel;
            panel?.Show(this.udEquip);
        }

        public void ShowEquipPicture()
        {
            // TODO 显示装备立绘界面
        }
    }
}
